﻿package scripts{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import com.greensock.TweenLite;
	import flash.filters.DropShadowFilter;

	public class Features extends MovieClip {
		private var currentPage:Number;
		private var displayNumber:Number;
		private var total:Number=6;
		private var arrowWidth:Number;
		private var gap:Number;
		private var graphicWidth:Number;
		private var container:MovieClip;
		private var allFeatures:Array;
		private var dropShadow:DropShadowFilter;
		public var btn__rightArrow:MovieClip;
		public var btn__leftArrow:MovieClip;
		public function Features( _displayNumber:Number,_currentPage:Number) {
			gap = 45;
			graphicWidth=210;
			arrowWidth=35;
			currentPage = 1;//_currentPage;
			displayNumber = _displayNumber;
			allFeatures = new Array();
			dropShadow = new DropShadowFilter();
			dropShadow.blurX = dropShadow.blurY = 8;
			dropShadow.alpha = 0.5;
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		private function init(e:Event):void{
			createLeftArrow();
			createRightArrow();
			addChild(createContainer());
			createMask();
			createAllFeatures();
			removeEventListener(Event.ADDED_TO_STAGE,init);
		}
		private function createAllFeatures():void{
			for(var i:int = 0;i<(displayNumber+2);i++)
			{
				var midPic:featureMC = new featureMC();
				midPic.x = -(graphicWidth + gap) + i*(graphicWidth + gap);
				midPic.y = 0;
				midPic.gotoAndStop(i + 1);
				container.addChild(midPic);
				allFeatures.push(midPic);
			}
		
		}
		private function leftMoveUpdate(e:Event):void
		{
			currentPage++;
			currentPage = currentPage%6;
			allFeatures.push(allFeatures.shift());
			for(var i:int = 0; i< (displayNumber+2);i++)
			{
				var id:int = (currentPage  + i)%total;
				allFeatures[i].gotoAndStop(id == 0?total:id);
				if(i == (displayNumber+1))
					allFeatures[i].x = - (graphicWidth + gap) + i*(graphicWidth + gap)
				else
					TweenLite.to(allFeatures[i],0.5,{x:(- (graphicWidth + gap) + i*(graphicWidth + gap)),alpha:1})
			}
		}
		private function rightMoveUpdate(e:Event):void
		{
			currentPage--;
			currentPage = (total + currentPage)%total;
			if(currentPage == 0)
				currentPage = total;
			allFeatures.unshift(allFeatures.pop());
			for(var i:int = 0; i< (displayNumber+2);i++)
			{
				var id:int = (currentPage  + i)%total;
				allFeatures[i].gotoAndStop(id==0?total:id);
				if(i == (0))
					allFeatures[i].x = - (graphicWidth + gap) + i*(graphicWidth + gap)
				else
					TweenLite.to(allFeatures[i],0.5,{x:(- (graphicWidth + gap) + i*(graphicWidth + gap)),alpha:1})
			}
		}
		private function createMask():void{
			var myMask:MovieClip = new maskMC();
			myMask.x = container.x;
			myMask.y = container.y;
			myMask.width = displayNumber * graphicWidth + (displayNumber - 1) * gap;
			myMask.height = 150;
			addChild(myMask);
			//myMask.alpha=0.6;
			container.mask = myMask;
		}
		private function createContainer():MovieClip{
			container = new MovieClip();
			container.x = arrowWidth+gap;
			container.y = -70;
			return container;
		}
		private function createRightArrow():void{
			btn__rightArrow= new rightArrow();
			btn__rightArrow.x = arrowWidth + (displayNumber + 1) * gap + displayNumber * graphicWidth;
			btn__rightArrow.useHandCursor=true;
			btn__rightArrow.buttonMode=true;
			btn__rightArrow.addEventListener(MouseEvent.CLICK,rightMoveUpdate);
			addChild(btn__rightArrow);
		}
		
		private function createLeftArrow():void{
			btn__leftArrow = new leftArrow();
			btn__leftArrow.x = 5;
			btn__leftArrow.buttonMode = true;
			btn__leftArrow.useHandCursor = true;
			btn__leftArrow.addEventListener(MouseEvent.CLICK,leftMoveUpdate);
			addChild(btn__leftArrow);
		}
	}	
}
